let utils = require('../util/utils');
let commonDao = require('../dao/commonDao');
let userDao = require('../dao/userDao');

let phoneTitleArr = ["134", "135", "136", "137", "138", "139", "150", "151", "157", "158", "159", "152", "136", "151", "156", "185", "181", "170", "186", "187", "188", "130", "131", "132", "155", "156", "189"];

let services = module.exports;

//随机生成名称
services.getRandomNickname = function () {
    return phoneTitleArr[utils.getRandomNum(0, phoneTitleArr.length - 1)] + utils.getRandomNum(10000000, 99999999);
};


//获取机器人
services.getRobotList = async function (unionID) {
    //获取一个空闲的机器人
    let userData = await userDao.getUserData({ unionID: unionID, isRobot: true, roomId: null });
    return userData;
}


//随机获取一个机器人
services.getRandomRobot = async function (unionID) {
    //获取一个空闲的机器人
    let userData = await userDao.getUserData({ unionID: unionID, isRobot: true, roomId: null });
    return userData;
}

//创建机器人
services.createRobot = async function () {

    let uid = null;
    let index = 1000;
    let robotUser = null;
    // 创建新账户
    while (index > 0) {
        uid = utils.getRandomNum(100000, 999999);
        let data = await commonDao.findOneData("accountModel", { uid: uid });
        if (!data) break;
        // 放置操作次数太多锁死
        index--;
        if (index <= 0) {
            uid = null;
            break;
        }
    }
    if (uid) {
        let nickname = this.getRandomNickname();
        // 更新用户信息
        let updateUserData = {
            syncLock: 0,
            nickname: nickname,
            uid: uid,
            isRobot: true,
            avatar: null,
            roomID: null
        };
        robotUser = await commonDao.findOneAndUpdateEx('userModel', { uid }, updateUserData, { upsert: true, new: true });
    }
    return robotUser;

}




